import React, { PureComponent } from 'react'
import { Icon, notification } from 'antd'
import { Header, getMenuList } from 'components'
import Modal from 'kenote-react-admin-modal'
import _ from 'lodash'
import PubSub from 'pubsub-js'
import history from 'store/history'
import { getPageFlag, getFlagValue } from '../services/utils'

const protoTestFlag = getPageFlag('/system-tools/proto', 'access')

export default class AdminHeader extends PureComponent {

  render () {
    const { auth, location, rtsp_keys, rtsp_key, actions, protoDataPending } = this.props
    const { pathname } = location
    const menus = [
      {
        key: '/system-tools',
        name: '系统工具',
        data: []
      },
      /*{
        key: 'help',
        name: '支持',
        data: [
          {
            key: '1031',
            name: '帮助与文档'
          },
          {
            key: '1032',
            name: '提交意见'
          },
          {
            key: '1033',
            name: '论坛'
          },
          {
            key: '1034',
            name: '博客'
          }
        ]
      },*/
      {
        key: 'auth0',
        name: `${auth && auth.nickname || auth.username || '\<admin\>'}`,
        data: [
          {
            key: '9004',
            name: '更新账号信息',
            onPress: () => actions.accessToken()
          },
          {
            type: 'driver'
          },
          /*{
            key: '9001',
            name: '基本资料',
            icon: (<Icon type="exception" />),
            onPress: () => {
              //PubSub.publish('OPEN_MODAL', 'ModifyPwdModal')
              history.push('/account/baseinfo')
            }
          },
          {
            key: '9002',
            name: '实名认证',
            icon: (<Icon type="contacts" />)
          },
          {
            key: '9003',
            name: '安全设置',
            icon: (<Icon type="setting" />),
            onPress: () => {
              //PubSub.publish('OPEN_MODAL', 'SettingSiderModal')
              history.push('/account/secure')
            }
          },
          {
            key: '9004',
            name: '安全管控',
            icon: (<Icon type="lock" />)
          },
          {
            key: '9005',
            name: '访问控制',
            icon: (<Icon type="idcard" />)
          },
          {
            key: '9006',
            name: 'accesskeys',
            icon: (<Icon type="key" />)
          },
          {
            key: '9007',
            name: '会员权益',
            icon: (<Icon type="solution" />)
          },
          {
            key: '9008',
            name: '会员积分',
            icon: (<Icon type="solution" />)
          },
          {
            key: '9009',
            name: '云大使管理',
            icon: (<Icon type="cloud-o" />)
          },
          {
            type: 'driver'
          },*/
          {
            key: '-1',
            name: '退出管理控制台',
            type: 'quit',
            onPress: () => {
              Modal.confirm({
                title: '确定要退出管理控制台吗？',
                onOk() {
                  return new Promise( (resolve, reject) => {
                    setTimeout(() => resolve(true), 500)
                  })
                  .then( ret => {
                    actions.logout()
                  })
                  
                }
              })
            }
          }
        ]
      },
    ]
    loadRouter('/system-tools', menus, auth)
    if (protoDataPending) {
      menus.splice(menus.length - 1, 0, {
        key: 'stop-proto',
        data: {
          component: (<span style={{ color: '#ff9933' }}><Icon type="close-circle-o" /> 终止请求</span>)
        },
        onPress: () => actions.stopProtoData()
      })
    }
    if (rtsp_keys && rtsp_keys.length > 0 && /^(\/proto)|(proto)$/.test(pathname)) {
      menus.splice(menus.length - 1, 0, {
        key: rtsp_key || rtsp_keys[0],
        name: '线路 :: ' + (rtsp_key || rtsp_keys[0]),
        data: rtsp_keys.map((item, i) => {
          return {
            key: item,
            name: item,
            onPress: () => {
              actions.setRutpKey(item)
            }
          }
        })
      })
    }
    return (
      <Header 
        {...{ auth, menus }}
        onPressItem={ ({ key }) => {
          let list = getMenuList(menus)
          let data = _.find(list, o => o.key === key)
          if (_.has(data, 'onPress')) {
            data.onPress()
          }
          else {
            notification.info({
              message: `Open Menu.Item`,
              description: `您点击了菜单 => [${data.key}]${data.name}`,
              duration: 8,
              placement: 'bottomRight'
            })
          }
        }}
      />
    )
  }
}

const loadRouter = (name, menus, auth) => {
  let siderMenu = _.find(menus, o => o.key === name)
  let Routes = _.find(window.Routes, o => o.path === name)
  Routes.childRoutes && Routes.childRoutes.map((item, i) => {
    let itemFlag = _.find(item.flags, o => o.key === 'access')
    if (!getFlagValue(auth, itemFlag.value, null) && siderMenu) {
      siderMenu['data'].push({
        key: `${name}/${item.path}`,
        name: item.name,
        onPress: () => history.push(`${name}/${item.path}`)
      })
    }
  })
  if (siderMenu['data'].length === 0) {
    _.remove(menus, o => o.key === name)
  }
}